//
//  WBStatusViewModel.swift
//  0829-Weibo
//
//  Created by 毕仲阳 on 16/9/3.
//  Copyright © 2016年 mycompany. All rights reserved.
//

import UIKit

class WBStatusViewModel: NSObject {
    var memberImage : UIImage?
    var avatarImage : UIImage?
    
    // 转发数
    var reposts_count: String?
    // 评论数量的字符串
    var comments_count: String?
    // 表态数量的字符串
    var attitudes_count: String?
    
    /// 转发微博的内容
    var retweetStautsText: String?
    
    /// 格式化好的来源字符串
    var sourceString: String?
    ///创建时间
    var createAtString: String? {
        guard let createAt = self.status?.created_at else {
            return nil
        }
        
        let formatter = DateFormatter()
        // 获取到当前的日历，日历对象有很多判断时间的方法
        let calendar = NSCalendar.current
        // 准备一个当前时间
        let currentDate = Date()
        
        if isDateInThisYear(target: createAt) {
            // 是否今天
            if calendar.isDateInToday(createAt) {
                // 是今天
                // 先求出当前时间与微博创建时间的秒数差值
                let result = currentDate.timeIntervalSince(createAt)
                print(result)
                if result < 60 {
                    return "刚刚"
                }else if result < 3600 {
                    return "\(Int(result) / 60)分钟前"
                }else {
                    return "\(Int(result) / 3600)小时前"
                }
            }else if calendar.isDateInYesterday(createAt) {
                // 是昨天
                formatter.dateFormat = "昨天 HH:mm"
                return formatter.string(from: createAt)
            }else {
                // 其他
                formatter.dateFormat = "MM-dd HH:mm"
                return formatter.string(from: createAt)
            }
        }else{
            // 不是今年
            formatter.dateFormat = "yyyy-MM-dd"
            return formatter.string(from: createAt)
        }
    }


    var status : WBStatus?{
        didSet {
            //计算会员图标
           if let mbrank = status?.user?.mbrank, mbrank > 0{
                let imagName = "common_icon_membership_level\(mbrank)"
            memberImage = UIImage(named: imagName)
            }
            //会员认证图标
            if let type = status?.user?.verified_type{
                switch type {
                case 1://大V
                    avatarImage = #imageLiteral(resourceName: "avatar_vip")
                case 2,3,5://企业
                    avatarImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
                case 220://达人
                    avatarImage = #imageLiteral(resourceName: "avatar_grassroot")
                default:
                    break
                }
            }
            reposts_count = self.translataCount(count: status?.reposts_count ?? 0, defaultTitle: "转发")
            comments_count = self.translataCount(count: status?.comments_count ?? 0, defaultTitle: "评论")
            attitudes_count = self.translataCount(count: status?.attitudes_count ?? 0, defaultTitle: "赞")
            
            // 计算转发微博的内容
            if let text = status?.retweeted_status?.text, let name = status?.retweeted_status?.user?.name{
                retweetStautsText = "@\(name):\(text)"
            }
            // <a href=\"http://app.weibo.com/t/feed/3722nD\" rel=\"nofollow\">微博树洞</a>
            // 来自 微博树洞
            if let source = status?.source{
                if let preRange = source.range(of: "\">"),let endRange = source.range(of: "</") {
                    let result = source.substring(with: preRange.upperBound..<endRange.lowerBound)
                    sourceString = "来自 \(result)"
                }
            }
            //创建时间
//            if let dateString = status?.created_at{
//                let formatter = DateFormatter()
//                formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
//                let result = formatter.date(from: dateString)
//                print(result)
//                
//            }
            //self.createAtString = calcCreateAtString(date: status?.created_at)

        }
    }
    /// 判断传入的date是否是与当前时间是同一个年份
    ///
    /// - parameter target: 目标年份
    private func isDateInThisYear(target: Date) -> Bool {
        
        let currentDate = Date()
        
        // 取到这两个时间的年份字符串
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy"
        
        // 取到目标日期的年份
        let targetYear = formatter.string(from: target)
        let currentYear = formatter.string(from: currentDate)
        
        return targetYear == currentYear
    }
    


    private func translataCount(count:Int,defaultTitle:String)->String{
        if count == 0 {
            return defaultTitle
        }else{
            if count < 10000{
                return "\(count)"
            }else
            {
                let result = count/1000
                let str = "\(CGFloat(result)/10)"
                let string = "\(str)万"
                return string.replacingOccurrences(of: ".0", with: "")
            }
        }
    }
}
